package com.fanshilz.flower2025.framework.aliyun.sms;

import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;

public class AliyunSmsUtil {

    public static Client createClient() throws Exception {
        Config config = new Config()
                // 配置 AccessKey ID，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
                .setAccessKeyId("LTAI5tHmSqoKrfmuexi5Wf4C")
                // 配置 AccessKey Secret，请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
                .setAccessKeySecret("568iXe9ZBvd3ZM9OCvEcfypl29e9np");

        // 配置 Endpoint
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    public static void sendVerificationCode(String code, String phoneNumber) {
        // 初始化请求客户端
        Client client;
        try {
            client = createClient();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 构造请求对象，请填入请求参数值
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumber)
                .setSignName("远境")
                .setTemplateCode("SMS_305067096")
                .setTemplateParam("{\"code\":\"" + code + "\"}");

        // 获取响应对象
        SendSmsResponse sendSmsResponse;
        try {
            sendSmsResponse = client.sendSms(sendSmsRequest);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 响应包含服务端响应的 body 和 headers
        System.out.println(JSONUtil.toJsonStr(sendSmsResponse));
    }

    public static void sendTemplateCode(String templateCode, String phoneNumber) {
        // 初始化请求客户端
        Client client;
        try {
            client = createClient();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 构造请求对象，请填入请求参数值
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phoneNumber)
                .setSignName("远境")
                .setTemplateCode(templateCode);

        // 获取响应对象
        SendSmsResponse sendSmsResponse;
        try {
            sendSmsResponse = client.sendSms(sendSmsRequest);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        // 响应包含服务端响应的 body 和 headers
        System.out.println(JSONUtil.toJsonStr(sendSmsResponse));
    }

//    public static void main(String[] args) throws Exception {
//        // 初始化请求客户端
//        Client client = createClient();
//
//        // 构造请求对象，请填入请求参数值
//        SendSmsRequest sendSmsRequest = new SendSmsRequest()
//                .setPhoneNumbers("17783738583")
//                .setSignName("远境")
//                .setTemplateCode("SMS_305067096")
//                .setTemplateParam("{\"code\":\"123876\"}");
//
//        // 获取响应对象
//        SendSmsResponse sendSmsResponse = client.sendSms(sendSmsRequest);
//
//        // 响应包含服务端响应的 body 和 headers
//        System.out.println(JSONUtil.toJsonStr(sendSmsResponse));
//
//    }

//    public static void main(String[] args) throws Exception {
//        sendTemplateCode("SMS_479070638", "17783738583");
//
//    }

}
